package com.ulook.lookplanet.ui.home.my;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.BaseRecyclerAdapter;
import com.ulook.lookplanet.base.BaseViewHolder;
import com.ulook.lookplanet.ui.square.VArticlePreInfo;
import com.ulook.lookplanet.widget.ArticleView;

public class MyArtActivity extends AppCompatActivity {

    private MyArtViewModel viewModel;
    private QMUIPullLayout pullLayout;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<VArticlePreInfo> mAdapter;
    private boolean isRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_art);

        viewModel = new ViewModelProvider(this).get(MyArtViewModel.class);

        Intent intent = getIntent();
        int currUserId = intent.getIntExtra("currUserId", 0);
        if(currUserId == 0){
            finish();
        }
        viewModel.setCurrUserId(currUserId);

        viewModel.getArtInfos().observe(this, articlePreInfos -> {
            if(isRefresh){
                mAdapter.setData(articlePreInfos);
                recyclerView.scrollToPosition(0);
            }else {
                mAdapter.append(articlePreInfos);
            }
        });

        initTopBar();
        initData();
    }

    /**
     * 初始化TopBar
     */
    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.my_art_bar);
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
        topBar.setTitle(R.string.my_article);
    }

    /**
     * 初始化Data
     */
    private void initData(){
        pullLayout = findViewById(R.id.my_art_pull_layout);
        recyclerView = findViewById(R.id.my_art_recyclerView);

        // 监听 上拉/下拉 动作
        pullLayout.setActionListener(pullAction -> pullLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) {
                    isRefresh = true;
                    viewModel.getArticleData(0);
                } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) {
                    isRefresh = false;
                    viewModel.getArticleData(mAdapter.getItemCount());
                }
                pullLayout.finishActionRun(pullAction);
            }
        }, 3000));
        // 设置布局
        recyclerView.setLayoutManager(new LinearLayoutManager(MyArtActivity.this) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        });
        // 初始化Adapter
        mAdapter = new BaseRecyclerAdapter<VArticlePreInfo>(MyArtActivity.this, null) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_article;
            }
            @Override
            public void bindData(BaseViewHolder holder, int position, VArticlePreInfo item) {
                ArticleView articleView = holder.getItemView(R.id.recycler_article_view);
                articleView.setWidgetData(item, viewModel.getCurrUserId());
            }
        };
        // 添加item点击动作
        /*mAdapter.setOnItemClickListener((itemView, pos) -> {
            VArticlePreInfo articlePreInfo = mAdapter.getItem(pos);
            Intent intent = new Intent(this, ArticleActivity.class);
            intent.putExtra("currUserId", currUserId);
            intent.putExtra("article", articlePreInfo);
            startActivity(intent);
        });*/
        // item长按动作
        mAdapter.setOnItemLongClickListener((itemView, pos) -> {
            // 弹框提醒
            new QMUIDialog.MessageDialogBuilder(MyArtActivity.this)
                    .setTitle("删除")
                    .setMessage("确定要删除这条动态吗？")
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction(0, "删除", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            viewModel.deleteArticle(mAdapter.getItem(pos).getUid(), pos);
                            mAdapter.remove(pos);
                            dialog.dismiss();
                        }
                    })
                    .show();
        });
        recyclerView.setAdapter(mAdapter);
        viewModel.getArticleData(0);
    }
}